import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()
export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/index.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/index.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    name: 'Dashboard',
    redirect: '/dashboard/index',
    meta: {},
    children: [
      {
        path: 'index',
        name: 'DashboardIndex',
        component: () => import('@/views/Dashboard/index.vue'),
        meta: {
          title: '首页',
          icon: 'bi:house-door'
        }
      }
    ]
  },
  {
    path: '/record',
    component: Layout,
    redirect: '/record/product/list',
    name: 'Record',
    meta: {
      title: '数据管理',
      icon: 'charm:chart-line',
      alwaysShow: true
    },
    children: [
      {
        path: 'product',
        name: 'Product',
        redirect: '/record/product/list',
        component: () => import('@/views/Record/Product/List/index.vue'),
        meta: {
          title: '商品管理',
          icon: 'ci:shopping-cart-02',
          alwaysShow: true
        },
        children: [
          {
            path: 'list',
            name: 'ProductList',
            component: () => import('@/views/Record/Product/List/index.vue'),
            meta: {
              title: '商品列表',
              btnPermission: []
            }
          },
          {
            path: 'classification',
            name: 'Classification',
            component: () => import('@/views/Record/Product/Classification/index.vue'),
            meta: {
              title: '分类管理',
              btnPermission: []
            }
          }
        ]
      },
      {
        path: 'franchiser',
        name: 'Franchiser',
        component: () => import('@/views/Record/Franchiser/Shop/index.vue'),
        meta: {
          title: '门店管理',
          icon: 'ci:user-02',
          alwaysShow: true
        },
        children: [
          {
            path: 'shop',
            name: 'Shop',
            component: () => import('@/views/Record/Franchiser/Shop/index.vue'),
            meta: {
              title: '门店列表',
              btnPermission: []
            }
          },
          {
            path: 'bonus',
            name: 'Bonus',
            component: () => import('@/views/Record/Franchiser/Bonus/index.vue'),
            meta: {
              title: '业务员列表',
              btnPermission: []
            }
          }
        ]
      },
      {
        path: 'thirdparty',
        name: 'Thirdparty',
        component: () => import('@/views/Record/Thirdparty/Third/index.vue'),
        meta: {
          title: '第三方管理',
          icon: 'lucide:home',
          alwaysShow: true
        },
        children: [
          {
            path: 'third',
            name: 'Third',
            component: () => import('@/views/Record/Thirdparty/Third/index.vue'),
            meta: {
              title: '第三方列表',
              btnPermission: []
            }
          }
        ]
      }
    ]
  },
  {
    path: '/storage',
    component: Layout,
    redirect: '/storage/warehouse/godown',
    name: 'Storage',
    meta: {
      title: '仓储管理',
      icon: 'material-symbols:add-home-work-outline',
      alwaysShow: true
    },
    children: [
      {
        path: 'warehouse',
        name: 'Warehouse',
        component: () => import('@/views/Storage/Warehouse/Godown/index.vue'),
        meta: {
          title: '仓库管理',
          icon: 'material-symbols:add-business-outline',
          alwaysShow: true
        },
        children: [
          {
            path: 'godown',
            name: 'Godown',
            component: () => import('@/views/Storage/Warehouse/Godown/index.vue'),
            meta: {
              title: '仓库列表',
              btnPermission: []
            }
          }
        ]
      }
    ]
  },
  {
    path: '/logistics',
    component: Layout,
    redirect: '/logistics/driver',
    name: 'Logistics',
    meta: {
      title: '物流管理',
      icon: 'bi:bus-front',
      alwaysShow: true
    },
    children: [
      {
        path: 'driver',
        name: 'Driver',
        component: () => import('@/views/Logistics/Driver/index.vue'),
        meta: {
          title: '司机管理',
          icon: 'bi:people',
          btnPermission: []
        }
      },
      {
        path: 'vehicle',
        name: 'Vehicle',
        component: () => import('@/views/Logistics/Vehicle/index.vue'),
        meta: {
          title: '车辆管理',
          icon: 'bi:truck-front',
          btnPermission: []
        }
      },
      {
        path: 'course',
        name: 'Course',
        component: () => import('@/views/Logistics/Course/index.vue'),
        meta: {
          title: '路线管理',
          icon: 'bi:geo-alt',
          btnPermission: []
        }
      },
      {
        path: 'waybill',
        name: 'Waybill',
        component: () => import('@/views/Logistics/Waybill/index.vue'),
        meta: {
          title: '运单管理',
          icon: 'bi:file-earmark-bar-graph',
          btnPermission: []
        }
      }
    ]
  },
  {
    path: '/document',
    component: Layout,
    redirect: '/document/purchase',
    name: 'Document',
    meta: {
      title: '单据管理',
      icon: 'bi:file-text',
      alwaysShow: true
    },
    children: [
      {
        path: 'purchase',
        name: 'Purchase',
        component: () => import('@/views/Document/Purchase/index.vue'),
        meta: {
          title: '采购单',
          icon: 'bi:journal-arrow-down',
          btnPermission: []
        }
      },
      {
        path: 'sales',
        name: 'Sales',
        component: () => import('@/views/Document/Sales/index.vue'),
        meta: {
          title: '销售单',
          icon: 'bi:journal-arrow-up',
          btnPermission: []
        }
      },
      {
        path: 'sorting',
        name: 'Sorting ',
        component: () => import('@/views/Document/Sorting/index.vue'),
        meta: {
          title: '分拣单',
          icon: 'bi:clipboard-data',
          btnPermission: []
        }
      }
    ]
  },
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/bill',
    name: 'Finance',
    meta: {
      title: '财务管理',
      icon: 'iconoir:dollar-circle',
      alwaysShow: true
    },
    children: [
      {
        path: 'bill',
        name: 'Bill',
        component: () => import('@/views/Finance/Bill/index.vue'),
        meta: {
          title: '账单列表',
          icon: 'bi:coin',
          btnPermission: []
        }
      },
      {
        path: 'accountName',
        name: 'AccountName',
        component: () => import('@/views/Finance/AccountName/index.vue'),
        meta: {
          title: '账户管理',
          icon: 'bi:currency-dollar',
          btnPermission: []
        }
      },
      {
        path: 'withdrawal',
        name: 'Withdrawal',
        component: () => import('@/views/Finance/Withdrawal/index.vue'),
        meta: {
          title: '提现记录',
          icon: 'bi:currency-exchange',
          btnPermission: []
        }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    redirect: '/settings/bill',
    name: 'Settings',
    meta: {
      title: 'OMS商城设置',
      icon: 'bi:gear',
      alwaysShow: true
    },
    children: [
      {
        path: 'miniProgram',
        name: 'MiniProgram',
        component: () => import('@/views/Settings/MiniProgram/index.vue'),
        meta: {
          title: '小程序设置',
          icon: 'bi:gear-fill',
          btnPermission: []
        }
      },
      {
        path: 'payment',
        name: 'Payment',
        component: () => import('@/views/Settings/Payment/index.vue'),
        meta: {
          title: '支付设置',
          icon: 'bi:currency-yen',
          btnPermission: []
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/account',
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'bi:bar-chart-line',
      alwaysShow: true
    },
    children: [
      {
        path: 'account',
        name: 'Account',
        component: () => import('@/views/System/Account/index.vue'),
        meta: {
          title: '账号管理',
          icon: 'ph:film-script',
          btnPermission: []
        }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/System/Role/index.vue'),
        meta: {
          title: '角色管理',
          icon: 'ph:user-circle-dashed-light',
          btnPermission: []
        }
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/System/Menu/index.vue'),
        meta: {
          title: '菜单管理',
          icon: 'ph:text-align-center-thin',
          btnPermission: []
        }
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
